package com.sky.controller.user;


import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Slf4j
@Api(tags = "地址薄")
public class AddressBookController {

    @Autowired
    AddressBookService addressBookService;
    /**
     * 查询所有地址
     * @return
     */
    @GetMapping ("/list")
    @ApiOperation("查询所有地址")
    public Result<List<AddressBook>> list(){
        List<AddressBook> list = addressBookService.list();
        return Result.success(list);
    }

    /**
     * 添加地址
     * @param addressBook
     * @return
     */
    @PostMapping
    @ApiOperation("添加地址")
    public Result add(@RequestBody AddressBook addressBook){
        addressBookService.addAddress(addressBook);
        return Result.success();
    }


    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result setDefault(@RequestBody AddressBook addressBook){
        addressBookService.setDefault(addressBook);
        return Result.success();
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> findById(@PathVariable Long id){
        AddressBook addressBook = addressBookService.findById(id);
        return Result.success(addressBook);
    }

    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    @ApiOperation("修改地址")
    public Result updateAddress(@RequestBody AddressBook addressBook){
        addressBookService.updateAddress(addressBook);
        return Result.success();
    }

    /**
     * 删除地址
     * @param addressBook
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除地址")
    public Result deleteAddress(@RequestBody AddressBook addressBook){
        addressBookService.deleteAddress(addressBook);
        return Result.success();
    }


    /**
     * 获取默认地址
     * @return
     */
    @GetMapping("default")
    @ApiOperation("获取默认地址")
    public Result<AddressBook> getDefaultAddress(){
        AddressBook addressBook = addressBookService.getDefaultAddress();
        return Result.success(addressBook);
    }

}
